// 粒子系统
class particleSystem {
  constructor() {
    this.emitRate = 0
    this.emitTime = 0
    this.maxCount = 0
    this.particleList = []
    this.instancedMesh = null
    this.particleMaterial = null
    this.emitAreaRotateMatrix = null
    this.geometry = new THREE.BufferGeometry()
    this.planeGeometry = new THREE.PlaneGeometry(1, 1)
    return this
  }
  onRefresh() {
    var t = this.config.basic
    var a = this.config.emit.rate
    var i = this.config.emit.area
    this.onDestroy()
    this.emitRate = a / 1e3
    this.maxCount = t.maxCount
    this.emitAreaRotateMatrix = new Qe
    this.emitAreaRotateMatrix.makeRotationFromEuler(new lt(Object(Qs.m)(i.rotate.x), Object(Qs.m)(i.rotate.y), Object(Qs.m)(i.rotate.z)));
    var r = this.config.basic.transform
    var o = r.translate
    var s = r.rotation
    var l = r.scale;
    this.position.set(o.x, o.y, o.z)
    this.rotation.set(Object(Qs.m)(s.x), Object(Qs.m)(s.y), Object(Qs.m)(s.z))
    this.scale.set(l.x, l.y, l.z);
    var u = this.context.assetsMgr.getTextureImage(t.texture)
    var c = new me(u);
    c.needsUpdate = !0;
    var h = new ls({
      uniforms: {
        map: {
          value: c
        }
      },
      vertexShader: "\nprecision highp float;\nuniform mat4 modelViewMatrix;\nuniform mat4 projectionMatrix;\nuniform mat3 normalMatrix;\nuniform mat4 viewMatrix;\nuniform mat4 modelMatrix;\n\nattribute mat4 instanceMatrix;\nattribute vec3 instanceColor;\nattribute vec3 position;\nattribute vec2 uv;\nattribute float opacity;\n\nvarying vec3 vColor;\nvarying vec2 vUv;\nvarying float vOpacity;\n\nvoid main() {\n    vUv = uv;\n    vColor = instanceColor;\n    vOpacity=opacity;\n\n    mat4 resetMatrix=mat4(viewMatrix*modelMatrix*instanceMatrix);\n\n    resetMatrix[0][0] = instanceMatrix[0][0]; \n    resetMatrix[0][1] = 0.0; \n    resetMatrix[0][2] = 0.0; \n\n    resetMatrix[1][0] = 0.0; \n    resetMatrix[1][1] = instanceMatrix[1][1]; \n    resetMatrix[1][2] = 0.0; \n\n    resetMatrix[2][0] = 0.0; \n    resetMatrix[2][1] = 0.0; \n    resetMatrix[2][2] = instanceMatrix[2][2];\n\n    gl_Position = projectionMatrix * resetMatrix * vec4( position, 1.0 );\n}\n",
      fragmentShader: "\nprecision highp float;\nuniform sampler2D map;\nvarying vec2 vUv;\nvarying vec3 vColor;\nvarying float vOpacity;\n\nvoid main() {\n    vec4 diffuseColor = texture2D( map, vUv );\n    gl_FragColor = diffuseColor*(vColor,vOpacity);\n    gl_FragColor*=vec4(vColor,vOpacity);\n}\n",
      blending: 2,
      depthTest: false,
      transparent: true
    });
    this.disposeList.push(h)
    this.particleMaterial = h
  }
  onDestroy() {
    this.clear()
    this.geometry.dispose()
    this.planeGeometry.dispose()
    this.particleList = []
  }
  getAssetsListMap() {
    return {
      HTMLImageElement: [this.config.basic.texture]
    }
  }
  emit() {
    var e = this.config
    var t = e.basic
    var n = t.lifeTime
    var a = e.emit.area
    var i = vc(a.x)
    var r = vc(a.y)
    var o = vc(a.z);
    if ("球形" == a.shape) {
      var s = Math.random() * Math.PI * 2
      var l = Math.random() * Math.PI;
      i = i * Math.sin(l) * Math.cos(s)
      r = r * Math.sin(l) * Math.sin(s)
      o *= Math.cos(l)
    } else if ("圆形" == a.shape) {
      var u = Math.random() * Math.PI * 2;
      i *= Math.cos(u)
      r *= Math.sin(u);
      var c = new _e(i, r, o = 0);
      c.applyMatrix4(this.emitAreaRotateMatrix)
      i = c.x
      r = c.y
      o = c.z
    }
    var h = new mc(1e3 * vc(n, !0), this.config);
    this.particleList.push(h)
    h.setPosition(i, r, o)
  }
  onUpdate(e) {
    this.updateEmit(e)
    this.updateParticles(e)
  }
  updateEmit(e) {
    if (this.particleList.length < this.maxCount) {
      this.emitTime += e;
      var t = this.emitRate * this.emitTime;
      if (t > this.maxCount && (t = this.maxCount - this.particleList.length), t > 1) {
        for (var n = 1; n <= t; n++)
          this.emit();
        this.emitTime = 0
      }
    } else {
      this.emitTime = 0
    }
  }
  updateParticles(e) {
    var t = this.particleList;
    this.geometry.dispose();
    if (this.instancedMesh) {
      this.remove(this.instancedMesh)
    }
    var n = new Kt;
    n.index = this.planeGeometry.index;
    var a = new Float32Array(t.length);
    n.attributes = this.planeGeometry.attributes;
    this.geometry = n;
    var i, r = new jr(this.planeGeometry, this.particleMaterial, t.length);
    this.add(r);
    this.instancedMesh = r;
    for (var o = 0; o < t.length; o++) {
      (i = t[o]).update(e)
      if (i.isDead) {
        this.removeParticle(i)
        o--;
      } else {
        var s = new Qe;
        s.compose(i.position, i.rotation, i.scale)
        r.setMatrixAt(o, s)
        r.setColorAt(o, i.color)
        a[o] = i.opacity
      }
    }
    n.setAttribute("opacity", new Rr(a, 1))
  }
  removeParticle(e) {
    this.particleList.splice(this.particleList.indexOf(e), 1)
  }
}